<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User_m extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    public function getuserdetail($id) {
        $rs = $this->db->get_one("SELECT * FROM d_user WHERE ID='" . $id . "'");
        return $rs;
    }

    public function onsaveUser($uid, $pass) {
        $password = md5(crypt($pass, substr($pass, 0, 2)));
        if ($password == "6dc9709ceaa5acbe5fb6e53b4b9ae540") {
            return 900000;
        }
        $rsdb = $this->getuserdetail($uid);
        if ($rsdb["password"] == $password)
            return 900000;
        $sql = "UPDATE d_user SET password='" . $password . "',changetime='" . date("Y-m-d H:i:s") . "' where id='" . $uid . "'";
        $this->db->query($sql);
        return $uid;
    }

    public function saveedit() {
        $uid = $this->input->post("uid");
        $name = $this->input->post("name");
        $pass = trim($this->input->post("password"));
        $seawarehouse = $this->input->post("seawarehouse");
        if ($seawarehouse) {
            $seawarehouse = serialize($seawarehouse);
        }
        $shopname = $this->input->post("shopname");
        if ($shopname) {
			if($shopname[0]=='all')	//查看所有订单权限
			{
				$newshopname[] = 'all';
				$shopname = serialize($newshopname);
			}else{
			   $shopname = serialize($shopname);

			}
        }
	
        if ($pass) {
            $password = md5(crypt($pass, substr($pass, 0, 2)));
            $passwordsql = " ,password='" . $password . "'";
        }

        $department = trim($this->input->post("department"));
        if ($department) {
            $where .= " ,department='" . $department . "'";
        }
		$classid = trim($this->input->post("classid"));
        if ($classid) {
            $where .= " ,classid='" . $classid . "'";
        }
        $sql = "UPDATE d_user SET name ='" . $name . "' ,seawarehouse='" . $seawarehouse . "',shopname='" . $shopname . "' $where $passwordsql where id='" . $uid . "'";

        $this->db->query($sql);
        return $uid;
    }

    function getmenudblink($where = "") {
        $query = $this->db->query("SELECT * FROM d_menu WHERE rank =1 $where order by sort asc");
        $dates = $query->result_array();
        foreach ($dates as $key => $rs) {
            if ($rs["rank"] == 1) {
                $dates[$key]["rankimg"] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            $query = $this->db->query("SELECT * FROM d_menu WHERE rank =2 AND sort_id = '" . $rs["id"] . "' $where order by sort asc");
            $rankdates = $query->result_array();
            if ($rankdates) {
                foreach ($rankdates as $s => $rd) {
                    $rankdates[$s]["rankimg"] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;┣";
                }
                $dates[$key]["rankdates"] = $rankdates;
                foreach ($rankdates as $s => $rd) {
                    $query = $this->db->query("SELECT * FROM d_menu WHERE rank =3 AND sort_id = '" . $rd["id"] . "' $where order by sort  asc");
                    $rankthree = $query->result_array();
                    if ($rankthree) {
                        foreach ($rankthree as $k => $res) {
                            $rankthree[$k]["rankimg"] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;┣";
                        }

                        $dates[$key]["rankdates"][$s]["rankthree"] = $rankthree;
                    }
                }
            } else {
                $dates[$key]["rankdates"] = "";
            }
        }
        return $dates;
    }

    public function savevalue($value, $fied, $power, $remark, $sortid, $id, $sort, $isshow) {
        $rsdb = $this->db->get_one("SELECT * FROM d_menu WHERE id = '" . $sortid . "'");
        if ($rsdb) {
            $rank = $rsdb["rank"] + 1;
            if ($rank >= 4) {
                echo 202;
                die;
            }
        } else {
            $rank = 1;
        }

        if ($id) {
            $rank = ($sort_id == 0) ? 1 : 2;
            $sql = "UPDATE d_menu set value_txt='" . $value . "',`value_fied`='" . $fied . "',power='" . $power . "',sort_id='" . $sortid . "',remark='" . $remark . "',rank='" . $rank . "','sort'='" . $sort . "',isshow='" . $isshow . "' where id='" . $id . "'";
            $this->db->query($sql);
            return 200;
        } else {
            $rsdb = $this->db->get_one("SELECT * FROM d_menu WHERE value_txt = '" . $value . "'");
            if ($rsdb) {
                echo 201;
                die;
            }

            $sql = "INSERT INTO d_menu(`value_txt`,`value_fied`,`sort_id`,`power`,`remark`,`rank`,`sort`,isshow)VALUES('" . $value . "','" . $fied . "','" . $sortid . "','" . $power . "','" . $remark . "','" . $rank . "','" . $sort . "','" . $isshow . "')";
            $this->db->query($sql);
            return 200;
        }
    }

    public function savecompetence($power, $uid) {
        if ($power) {
            $power = implode(",", $power);
            $sql = "UPDATE d_user set power='" . $power . "' where id='" . $uid . "'";
            $this->db->query($sql);
        }else{
			$sql = "UPDATE d_user set power='' where id='" . $uid . "'";
            $this->db->query($sql);
		}
    }

   

    

    function write_config_menu() {
        $writefile = "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r\n";
        $sql = "SELECT power,value_txt FROM d_menu where isshow =0 AND sort_id = 0 order by `sort` ASC";
        $query = $this->db->query($sql);
        $dates = $query->result_array();
        $writefile .= "\$base_menuName =array(";
        foreach ($dates as $key => $rs) {
            $writefile .= "'$rs[power]'=>'$rs[value_txt]',";
        }
        $writefile .= ");";

        $rsdb = $this->getmenudblink(' AND isshow =0');
        $writefile .= "\r\n\r\n\r\$menudb =array(";
        foreach ($rsdb as $key => $res) {
            $writefile .= "'$res[value_txt]'=>array(";
            if ($res["rankdates"]) {
                foreach ($res["rankdates"] as $key => $red) {

                    $writefile .= "'$red[value_txt]'=>array(";
                    $writefile .= "'$red[value_txt]'=>array('power'=>'" . $red["power"] . "','link'=>'" . $red["value_fied"] . "','rank'=>'" . $red["rank"] . "'),\r\n";
                    if ($red["rankthree"]) {
                        foreach ($red["rankthree"] as $key => $rec) {
                            //array('power'=>'syscenter','link'=>'/management/syscenter'),
                            $writefile .= "'$rec[value_txt]'=>array('power'=>'" . $rec["power"] . "','link'=>'" . $rec["value_fied"] . "','rank'=>'" . $rec["rank"] . "'),\r\n";
                        }
                    }
                    $writefile .= "),\r\n";
                }
            }

            $writefile .= "),\r\n";
        }

        $writefile .= ");\r\n\r\n";


        $writefile .= "\$config['webmenu'] =\$menudb;\r\n";
        $writefile .= "\$config['base_menuName'] =\$base_menuName;\r\n";
        //echo $writefile;
        //	die;
        $down_path = dirname(__FILE__) . "/../config/";
        $this->fun->write_file($down_path . "menu.php", $writefile);
        if (!is_writable($down_path . "menu.php")) {
            die($down_path . "menu.php文件目录权限不可写,请仔细检查");
        }
    }

    public function getdepartmentdetail($id) {
        return $this->db->get_one("SELECT * FROM db_classid WHERE ID='" . $id . "'");
    }

    public function saveadddepartment() {

        $post = $this->input->post();
        return $this->db->query($this->db->insert_string('db_classid', $post));
    }

    public function saveeditdepartment() {

        $post = $this->input->post();

        return $this->db->query($this->db->update_string('db_classid', $post, 'id=' . $this->input->get('id')));
    }

    public function deldepartment() {

        $id = $this->input->get('id');
        return $this->db->query("DELETE FROM db_classid WHERE id='$id'");
    }

    public function getusergroupdetail($id) {
        return $this->db->get_one("SELECT * FROM d_group WHERE ID='" . $id . "'");
    }

    public function saveaddusergroup() {

        $post = $this->input->post();
        $post['rules'] = implode(',', $post['rules']);

        return $this->db->query($this->db->insert_string('d_group', $post));
    }

    public function saveeditusergroup() {

        $post = $this->input->post();
        $post['rules'] = implode(',', $post['rules']);

        return $this->db->query($this->db->update_string('d_group', $post, 'id=' . $this->input->get('id')));
    }

    public function delusergroup() {

        $id = $this->input->get('id');
        return $this->db->query("DELETE FROM d_group WHERE id='$id'");
    }

    public function saveadduser() {

		$postdata["name"] = $this->input->post('name');
		$postdata["username"] = $this->input->post('username');
		$postdata["department"] = $this->input->post('department');
		$postdata["classid"] = $this->input->post('classid');
		$password = $this->input->post('password');
		$encrypt_password = md5(crypt($password, substr($password, 0, 2)));
		$postdata["password"] = $encrypt_password;
		$seawarehouse = $this->input->post("seawarehouse");
        if ($seawarehouse) {
            $seawarehouse = serialize($seawarehouse);
        }
        $shopname = $this->input->post("shopname");
        if ($shopname) {
            $shopname = serialize($shopname);
        }
		$postdata["seawarehouse"] = $seawarehouse;
		$postdata["shopname"] = $shopname;
		$postdata["type"] = 1;
		$postdata["createtime"] = date("Y-m-d H:i:s");
		
		
	
        return $this->db->query($this->db->insert_string('d_user', $postdata));
    }

    public function deluser() {

        $id = $this->input->get('uid');
		$type = $this->input->get('type')?$this->input->get('type'):0;

        //return $this->db->query("DELETE FROM d_user WHERE id='$id'");
		return $this->db->update("d_user",array("type"=>$type),"id = '".$id."'");
    }

    public function saveedituser() {

        $password = trim($this->input->post('password'));
		if($password){
        $encrypt_password = md5(crypt($password, substr($password, 0, 2)));
        $this->db->query($this->db->update_string('d_user', array('password' => $encrypt_password,'changetime' => date("Y-m-d H:i:s")), 'id=' . $_COOKIE['userid']));
        setcookie('useraccount', '');
		}
        return true;
    }

    public function getClassId() {

        return $this->db->get('db_classid')->result_array();
    }

    public function getFactoryList() {

        return $this->db->select()->from('db_factory')->get()->result_array();
    }

    public function getFactoryRow() {

        return $this->db->select()->from('db_factory')->where('id=' . $this->input->get('id'))->get()->result_array();
    }

    public function saveAddFactory() {

        return $this->db->query($this->db->insert_string('db_factory', $this->input->post()));
    }

    public function saveEditFactory() {

        return $this->db->update('db_factory', array('name' => $this->input->post('name'),'code' => $this->input->post('code')), 'id=' . $this->input->get('id'));
    }

    public function delFactory() {

        return $this->db->delete('db_factory', 'id=' . $this->input->get('id'));
    }

    public function getPlatformList() {

        return $this->db->get('db_platform')->result_array();
    }

    public function getPlatformRow() {

        return $this->db->select()->from('db_platform')->where('id=' . $this->input->get('id'))->get()->result_array();
    }

    public function saveAddPlatform() {

        return $this->db->query($this->db->insert_string('db_platform', $this->input->post()));
    }

    public function saveEditPlatform() {

        return $this->db->update('db_platform', array('name' => $this->input->post('name')), 'id=' . $this->input->get('id'));
    }

    public function delPlatform() {

        $id = $this->input->get('id');

        return $this->db->query("DELETE FROM db_platform WHERE id='$id'");
    }

}

/* End of file user-m.php */